package org.dromara.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsOrderType;
import org.dromara.wms.mapper.WmsOrderTypeMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 仓储作业类别表Service业务层处理
 *
 * @author wenrui
 * @date 2024-03-21
 */
@RequiredArgsConstructor
@Service
public class WmsOrderTypeServiceImpl extends BaseService {

  private final WmsOrderTypeMapper baseMapper;

  /**
   * 查询仓储作业类别表
   */
  public WmsOrderType queryById(Long typeId) {
    return baseMapper.selectById(typeId);
  }

  /**
   * 查询仓储作业类别表列表
   */
  public TableDataInfo<WmsOrderType> queryPageList(WmsOrderType bo, PageQuery pageQuery) {
    LambdaQueryWrapper<WmsOrderType> lqw = buildQueryWrapper(bo);
    Page<WmsOrderType> result = baseMapper.selectPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  /**
   * 查询仓储作业类别表列表
   */
  public List<WmsOrderType> queryList(WmsOrderType bo) {
    LambdaQueryWrapper<WmsOrderType> lqw = buildQueryWrapper(bo);
    return baseMapper.selectList(lqw);
  }

  private LambdaQueryWrapper<WmsOrderType> buildQueryWrapper(WmsOrderType bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<WmsOrderType> lqw = Wrappers.lambdaQuery();
    lqw.like(StringUtils.isNotBlank(bo.getName()), WmsOrderType::getName, bo.getName());
    lqw.eq(StringUtils.isNotBlank(bo.getWmsType()), WmsOrderType::getWmsType, bo.getWmsType());
    lqw.eq(bo.getBackId() != null, WmsOrderType::getBackId, bo.getBackId());
    return lqw;
  }

  /**
   * 新增仓储作业类别表
   */
  public Boolean insertByBo(WmsOrderType bo) {
    validEntityBeforeSave(bo);
    boolean flag = baseMapper.insert(bo) > 0;
    return flag;
  }

  /**
   * 修改仓储作业类别表
   */
  public Boolean updateByBo(WmsOrderType bo) {
    validEntityBeforeSave(bo);
    return baseMapper.updateById(bo) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validEntityBeforeSave(WmsOrderType entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除仓储作业类别表
   */
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return baseMapper.deleteBatchIds(ids) > 0;
  }
}
